#include <xp/bas/arr.h>
#include <xp/bas/stdio.h>

#ifdef __linux
	#include <mcheck.h>
#endif

xp_ssize_t print_datum (xp_arr_t* arr, xp_size_t index, void* datum)
{
	xp_printf (XP_TEXT("%d "), *(xp_int64_t*)datum);
	return 1;
}


int xp_main (void)
{
	int i;
	xp_arr_t* arr;

#ifdef __linux
	mtrace ();
#endif

	arr = xp_arr_open (XP_NULL, 1, xp_sizeof(int), XP_NULL);
	if (arr == XP_NULL) {
		xp_printf (XP_TEXT("can't create arr\n"));
		return -1;
	}

	for (i = 0; i < 20; i++) {
		int k = i * 12;
		int* x;
		if ((x = (int*)xp_arr_adddatum (arr, &k)) == XP_NULL) {
			xp_arr_close (arr);
			xp_printf (XP_TEXT("can't add datum\n"));
			return -1;
		}

		xp_printf (XP_TEXT("added %d\n"), *x);
	}

	xp_printf (XP_TEXT("capacity:%u size:%u\n"), XP_ARR_CAPA(arr), XP_ARR_SIZE(arr));
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("\n------------------------\n"));

	for (i = 0; i < arr->size; i++) {
		if (i % 2) {
			xp_printf (XP_TEXT("removing %d\n"), 
				*((int*)xp_arr_getdatum(arr, i)));
			xp_arr_remrange (arr, i, 1);
		}
	}

	xp_printf (XP_TEXT("capacity:%u size:%u\n"), 
		XP_ARR_CAPA(arr), XP_ARR_SIZE(arr));
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("\n------------------------\n"));

	xp_arr_close (arr);
	return 0;
}
